package itdep.ui.base.pages;

import ga.i18n.Translatable;
import ga.i18n.Translation;
import ga.i18n.TranslationService;
import ga.tapestry.StateConstants;
import ga.tapestry.commonlib.base.pages.AbstractPage;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.grid.GridDataSource;
import org.apache.tapestry5.internal.grid.CollectionGridDataSource;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Ivan Khalopik
 * @version $Revision: 56 $ $Date: 2010-07-07 10:56:24 +0000 (Wed, 07 Jul 2010) $
 */
public abstract class TranslatableEnumPage<E extends Enum<E>> extends AbstractPage {

	private String state;
	private E record;
	private E row;

	private Map<String, Translation> translations;

	private GridDataSource dataSource;

	@Inject
	private TranslationService translationService;

	@Component
	private Form detailsForm;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public E getRecord() {
		return record;
	}

	public void setRecord(E record) {
		this.record = record;
	}

	public E getRow() {
		return row;
	}

	public void setRow(E row) {
		this.row = row;
	}

	public boolean isEditable() {
		final String state = getState();
		return StateConstants.ADD_STATE.equals(state) || StateConstants.EDIT_STATE.equals(state);
	}

	public Map<String, Translation> getTranslations() {
		return translations;
	}

	public GridDataSource getDataSource() {
		if (dataSource == null) {
			dataSource = new CollectionGridDataSource(getValues());
		}
		return dataSource;
	}

	protected void onActivate(EventContext context) {
		final String state = context.get(String.class, 0);
		final E record = context.get(getEnumClass(), 1);
		setState(state);
		if (StateConstants.VIEW_STATE.equals(state) || StateConstants.EDIT_STATE.equals(state)) {
			//noinspection unchecked
			setRecord(record);
			translations = translationService.getTranslations((Translatable) getRecord(), getLocale());
		} else {
			setRecord(null);
		}
	}

	protected List<?> onPassivate() {
		return CollectionFactory.newList(getState(), getRecord());
	}

	protected void onView(EventContext context) {
		if (context.getCount() > 0) {
			final E record = context.get(getEnumClass(), 0);
			setRecord(record);
		}
		setState(StateConstants.VIEW_STATE);
	}

	protected void onEdit(EventContext context) {
		if (context.getCount() > 0) {
			final E record = context.get(getEnumClass(), 0);
			setRecord(record);
		}
		setState(StateConstants.EDIT_STATE);
	}

	protected void onCancel() {
		setRecord(null);
		setState(null);
	}

	protected void onSave() {
		if (detailsForm.isValid()) {
			translationService.saveTranslations((Translatable) getRecord(), translations);
			setState(StateConstants.VIEW_STATE);
		}
	}

	protected List<E> getValues() {
		return Arrays.asList(getEnumClass().getEnumConstants());
	}

	protected abstract Class<E> getEnumClass();

}
